# -*- coding: utf-8 -*-

def combine(first_sub, second_sub,
        second_sub_start=0, skip=(), forward=(), backward=(),
        first_sub_behind_second_sub=True):
    """Combine first subtitle and second subtitle based on first subtitle time line.

    When first subtitle index takes 1 step forward, there are 4 options
    for second subtitle index:
    1) keep pace with first subtitle index (default)
    2) stay unchanged (skip)
    3) take 1 step forward (forward)
    4) take 1 step backward (backward)
    """
    second_idx = second_sub_start - 1 if second_sub_start > 1 else 0
    first_blocks = get_blocks(first_sub)
    second_blocks = get_blocks(second_sub)
    blocks = []

    if skip:
        skip = reindex(skip)
    if forward:
        forward = reindex(forward)
    if backward:
        backward = reindex(backward)

    for i in range(len(first_blocks)):
        sid = first_blocks[i][0]
        time = first_blocks[i][1]
        second_part = ''
        first_part = first_blocks[i][2]
        ignore = False
        if second_idx < len(second_blocks): # second sub not ends
            if i in skip: # second sub moves downward
                ignore = True
            elif i in forward: # second sub moves upward and combine
                # change last second sub by merging it with current second sub
                blocks[-1][2] = block[2] + second_blocks[second_idx][2]
                second_idx += 1 # step forward
            elif i in backward: # repeat last second sub
                second_idx -= 1 # step backward
            if not ignore:
                second_part = second_blocks[second_idx][2]
                second_idx += 1 # move cursor
        block = make_block(sid, time, second_part, first_part,
            first_sub_behind_second_sub)
        blocks.append(block)
    return blocks

def make_block(sid, time, second_part, first_part, first_sub_behind_second_sub):
    """Make a subtitle block.

    Each block consists of:
    1) part id
    2) time line
    3) first subtitle content
    4) second subtitle content
    """
    block = []
    block.append(sid)
    block.append(time)
    if first_sub_behind_second_sub:
        block.append(second_part)
        block.append(first_part + '\n')
    else:
        block.append(first_part)
        block.append(second_part + '\n')
    return block

def reindex(index):
    return [i-1 for i in index]

def get_blocks(sub):
    """Read each part of a subtitle file and make them block.

    A block consists of 1) part id 2) time line 3) part content.
    """
    with open(sub) as f:
        allblocks = []
        block = []
        idx = 0
        content = ''
        for line in f:
            if not line.isspace(): # not empty line
                if idx < 2: # sid and time
                    block.append(line)
                else: # subtitle
                    content += line
                idx += 1
            else: # empty line
                if block: # ensure it's not successive empty line
                    block.append(content)
                    allblocks.append(block)
                    idx = 0
                    block = []
                    content = ''
        if block: # in case of no empty line at the end of file
            block.append(content)
            allblocks.append(block)
            idx = 0
            block = []
            content = ''
        return allblocks

if __name__ == '__main__':
    import conf
    blocks = combine(conf.first_sub, conf.second_sub,
        second_sub_start=conf.second_sub_start,
        skip=conf.down, forward=conf.up, backward=conf.repeat,
        first_sub_behind_second_sub=conf.first_sub_behind_second_sub)
    with open(conf.combined_sub, 'w') as f:
        lines = ''
        for block in blocks:
            lines += ''.join(block)
        f.write(lines)